import { FormType, ModalOpacity } from "../../UIFrame/config/SysDefine";
import { MaskType } from "../../UIFrame/FrameType";
import { Event_Name, GEventManager } from "../../UIFrame/GEventManager";
import LocalStorageData from "../../UIFrame/LocalStorageData";
import SoundManager from "../../UIFrame/SoundManager";
import UIBase from "../../UIFrame/UIBase";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SettingUI extends UIBase {

    @property(cc.Node)
    soundNode: cc.Node = null;

    @property(cc.Node)
    musicNode: cc.Node = null;

    @property(cc.Node)
    vibrationNode: cc.Node = null;

    formType = FormType.PopUp;
    maskType = new MaskType(ModalOpacity.OpacityHalf, false);

    static prefabPath = "prefabs/UIForms/SettingUI";

    onShow() {
        this.refushUI();
    }

    refushUI() {
        let obj = SoundManager.getInstance().getVolumeToLocal();
        if (obj) {
            if (obj.effectVolume > 0) {
                this.setBtnStatus(1, true);
            } else {
                this.setBtnStatus(1, false);
            }
            if (obj.musicVolume > 0) {
                this.setBtnStatus(2, true);
            } else {
                this.setBtnStatus(2, false);
            }
            let vibration = LocalStorageData.Instance().getTypeData('vibration');
            if (vibration) {
                this.setBtnStatus(3, true);
            } else {
                this.setBtnStatus(3, false);
            }

        }
    }

    setBtnStatus(id, isOn) {
        let index = 35;
        if (!isOn) index = -35;
        if (id == 1) {
            this.soundNode.position = cc.v3(index, 0, 0);
        } else if (id == 2) {
            this.musicNode.position = cc.v3(index, 0, 0);
        } else if (id == 3) {
            this.vibrationNode.position = cc.v3(index, 0, 0);
        }
    }

    onSoundBtnClick() {
        GEventManager.emit(Event_Name.GAME_PLAY_SOUNDS, 'sounds/click');
        let obj = SoundManager.getInstance().getVolumeToLocal();
        if (obj) {
            if (obj.effectVolume > 0) {
                this.setBtnStatus(1, false);
                SoundManager.getInstance().setEffectVolume(0);
            } else {
                this.setBtnStatus(1, true);
                SoundManager.getInstance().setEffectVolume(0.5);
            }
        }
    }

    onMusicBtnClick() {
        GEventManager.emit(Event_Name.GAME_PLAY_SOUNDS, 'sounds/click');

        let obj = SoundManager.getInstance().getVolumeToLocal();
        if (obj) {
            if (obj.musicVolume > 0) {
                this.setBtnStatus(2, false);
                SoundManager.getInstance().setMusicVolume(0);
            } else {
                this.setBtnStatus(2, true);
                SoundManager.getInstance().setMusicVolume(0.5);
            }
        }
    }

    onVibrationBtnClick() {
        GEventManager.emit(Event_Name.GAME_PLAY_SOUNDS, 'sounds/click');

        let vibration = LocalStorageData.Instance().getTypeData('vibration');
        if (vibration) {
            this.setBtnStatus(3, false);
            LocalStorageData.Instance().setTypeData('vibration', false);
        } else {
            this.setBtnStatus(3, true);
            LocalStorageData.Instance().setTypeData('vibration', true);
        }
    }

    onClickEvent_closeUI() {
        this.closeUIForm();
    }

}
